home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex19.c < prev    next >
C/C++ Source or Header  |  1995-04-24  |  5KB  |  117 lines

  1. #include <genstub.c>
  2.  
  3. #define IDM_LAUNCH  201
  4. #define IDM_READ    202
  5. #define IDM_WRITE   203
  6. #define IDM_FLUSH   204
  7.  
  8. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  9. {
  10.    static LPVOID lpHeapData = NULL;
  11.    static LPVOID lpExtData = NULL;
  12.    static DWORD  dwDebuggeeProcessID = 0;
  13.  
  14.    switch (uMsg)
  15.    {
  16.          case WM_CREATE:
  17.          {
  18.                DefWindowProc(hWnd, uMsg, wParam, lParam);
  19.                // allocate some heap data for exchange
  20.                lpHeapData = HeapAlloc( GetProcessHeap(), 0, 256 );
  21.                // if child set child name.
  22.                if ( strstr( GetCommandLine(), "DEBUGGEE" ) )
  23.                {
  24.                   SetWindowText( hWnd, "DEBUGGEE" );
  25.                   dwDebuggeeProcessID = GetCurrentProcessId( );
  26.                   lstrcpy( lpHeapData, "@@@ Debuggee Data @@@");
  27.                   SetMenu( hWnd, 0 );  // Debuggee doesn't need a menu.
  28.                }
  29.                else
  30.                { // position parent above child
  31.                   SetWindowText( hWnd, "DEBUGGER" );
  32.                   lstrcpy(lpHeapData, "<<<< Debugger Data >>>>");
  33.                }
  34.          }
  35.          break;
  36.          case WM_DESTROY:
  37.                HeapFree( GetProcessHeap(), 0, lpHeapData );
  38.                PostQuitMessage( 0 );
  39.                break;
  40.          case WM_COMMAND:       // process menu items
  41.             {
  42.                TCHAR szBuffer[128];
  43.                switch ( LOWORD( wParam )  )
  44.                {
  45.                      case IDM_LAUNCH:
  46.                         {
  47.                            STARTUPINFO si;
  48.                            PROCESS_INFORMATION pi;
  49.                            HWND hWndEx;
  50.  
  51.                            // only one can be debugged at a time.
  52.                            if (FindWindow(lpszAppName, "DEBUGGEE")!=0)
  53.                               break;
  54.                            ZeroMemory(&si, sizeof(STARTUPINFO));
  55.                            ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
  56.                            si.cb=sizeof(STARTUPINFO);
  57.                            CreateProcess( "E:\\WAITE\\CHAP25\\GENERIC.EXE", "DEBUGGEE", NULL, NULL, FALSE,
  58.                                       0, NULL, NULL, &si, &pi );
  59.                            dwDebuggeeProcessID = pi.dwProcessId;
  60.                            // Let initialization of new process occur.
  61.                            if ( pi.dwProcessId )
  62.                            {
  63.                               WaitForInputIdle( GetCurrentProcess(), INFINITE );
  64.                               while ((hWndEx = FindWindow(lpszAppName, "DEBUGGEE"))==0);
  65.                               lpExtData = (LPVOID) SendMessage(hWndEx, WM_USER, 0, 0);
  66.                            }
  67.                      }
  68.                      break;
  69.                      case IDM_READ:
  70.                         {
  71.                            TCHAR szData[256];
  72.                            DWORD dwBytesRead = 0;
  73.                            HANDLE hDebuggee = OpenProcess( PROCESS_VM_READ, FALSE,
  74.                                                            dwDebuggeeProcessID );
  75.                            ReadProcessMemory( hDebuggee, lpExtData,
  76.                                               szData, 255, &dwBytesRead );
  77.                            wsprintf( szBuffer, "%ld Bytes Read. Data=%s",
  78.                                      dwBytesRead, szData );
  79.                            MessageBox( hWnd, szBuffer, "Reading Process", MB_OK );
  80.                            CloseHandle( hDebuggee );
  81.                         }
  82.                      break;
  83.                      case IDM_WRITE:
  84.                         {
  85.                            DWORD dwBytesWritten = 0;
  86.                            HANDLE hDebuggee = OpenProcess( PROCESS_VM_WRITE, FALSE,
  87.                                                            dwDebuggeeProcessID );
  88.                            WriteProcessMemory( hDebuggee, lpExtData,
  89.                                                lpHeapData, 255, &dwBytesWritten );
  90.                            wsprintf(szBuffer, "%ld Bytes Written", dwBytesWritten);
  91.                            MessageBox( hWnd, lpExtData, "Writing Process", MB_OK );
  92.                            CloseHandle( hDebuggee );
  93.                         }
  94.                         break;
  95.                      case IDM_FLUSH:
  96.                         {
  97.                            HANDLE hDebuggee = OpenProcess( PROCESS_VM_WRITE, FALSE,
  98.                                                            dwDebuggeeProcessID );
  99.                            wsprintf( szBuffer, "Flush returns: %d ",
  100.                                      FlushInstructionCache( hDebuggee, NULL, 100 ) );
  101.                            MessageBox( hWnd, szBuffer, "FlushInstructionCache()", MB_OK );
  102.                            CloseHandle( hDebuggee );
  103.                         }
  104.                         break;
  105.                      case IDM_EXIT:
  106.                            DestroyWindow( hWnd );
  107.                            break;
  108.                }
  109.             }
  110.             break;
  111.          case WM_USER: // Used to return value of the heap data.
  112.                return lpHeapData;
  113.          default:
  114.                return DefWindowProc( hWnd, uMsg, wParam, lParam );
  115.    }
  116.    return NULL;
  117. }